#!/bin/bash
#
# Build ARM kernel 4.x.y and 5.x.y for
# QEMU Raspberry Pi Emulation
#
#######################################################

TOOLCHAIN=arm-linux-gnueabihf
COMMIT=""
# Change to true if you want to install the packages
INSTALL_PACKAGES=
# Change to true if you want to clone the repo (takes longer)
USE_GIT=
USB_WEBCAM_MODULES=""    # add USB & V4L modules for USB webcam support (didn't work as static)
KERNEL_MAKE_CONFIG=menuconfig # set to olddefconfig to skip user prompting
KERNEL_EXTRA_CONFIG=""

SOURCE_DIR=$(pwd)
BUILD_DIR=$SOURCE_DIR
TARGET_DIR=$SOURCE_DIR

if [ -f build-kernel-qemu.conf ] ; then
	. build-kernel-qemu.conf
fi


if [ -n "$INSTALL_PACKAGES" ] ; then
	echo "NOT EMPTY"
	sudo apt-get update
	sudo apt-get install git libncurses5-dev gcc-arm-linux-gnueabihf flex bison
fi

mkdir -p $BUILD_DIR $TARGET_DIR
CUR_DIR=$(pwd)
cd $BUILD_DIR

if [ -n "$USE_GIT" ] ; then
	# checking out 4.x.y tag - change it if you want to change kernel version
	# for a specific hash, have a look at: https://github.com/raspberrypi/linux/tags
	# To check the effective kernel version, click on a tag in that list;
	# the branch and tag name are under the commit message.
	if [ ! -d linux ] ; then
		git clone https://github.com/raspberrypi/linux.git
	else
		echo "Using existing Linux sources!"
	fi
	cd linux
	if [ "$COMMIT" ] ; then
		git reset --hard
		git clean -xdf
		git checkout "$COMMIT"
	fi
else
	if [ -z "$COMMIT" ] ; then echo "COMMIT missing!" >&2 ; exit 1 ; fi
	wget -c https://github.com/raspberrypi/linux/archive/${COMMIT}.tar.gz -O linux-${COMMIT}.tar.gz
	rm -rf linux-${COMMIT}
	tar -xzvf linux-${COMMIT}.tar.gz
  cd linux-${COMMIT}
fi

# Use all available cores for compilation
export MAKEFLAGS="$MAKEFLAGS -j$(nproc)"

KERNEL_VERSION=$(make kernelversion)
KERNEL_TARGET_FILE_NAME=$TARGET_DIR/qemu-kernel-$KERNEL_VERSION
MODULES_INSTALL_PATH=$TARGET_DIR/qemu-kernel-$KERNEL_VERSION-modules
if [ -e "../linux-arm-$KERNEL_VERSION.patch" ]; then
    patch -p1 < $SOURCE_DIR/linux-arm-$KERNEL_VERSION.patch
else
    patch -p1 < $SOURCE_DIR/linux-arm.patch
fi
make ARCH=arm versatile_defconfig
echo "Building Qemu Raspberry Pi kernel qemu-kernel-$KERNEL_VERSION"

cat >> .config << EOF
CONFIG_CROSS_COMPILE="$TOOLCHAIN"
EOF

if [ -e "$SOURCE_DIR/config_file_$KERNEL_VERSION" ]; then
    cat "$SOURCE_DIR/config_file_$KERNEL_VERSION" >> .config
else
    cat "$SOURCE_DIR/config_file" >> .config
fi

if [ -e "$KERNEL_EXTRA_CONFIG" ]; then
    cat "$KERNEL_EXTRA_CONFIG" >> .config
fi

if [ $USB_WEBCAM_MODULES ] ; then
    echo "Make sure you have drivers for your webcam selected in menuconfig"
    cat $SOURCE_DIR/config_webcam >> .config
fi

cat $SOURCE_DIR/config_ip_tables >> .config
cat $SOURCE_DIR/config_loopdev >> .config

make -k ARCH=arm CROSS_COMPILE=${TOOLCHAIN}- $KERNEL_MAKE_CONFIG
make -k ARCH=arm CROSS_COMPILE=${TOOLCHAIN}- bzImage dtbs
cp arch/arm/boot/zImage $KERNEL_TARGET_FILE_NAME
if [ -e arch/arm/boot/dts/versatile-pb.dtb ] ; then
    cp arch/arm/boot/dts/versatile-pb.dtb $TARGET_DIR
fi

if [ $USB_WEBCAM_MODULES ] ; then
    mkdir -p $MODULES_INSTALL_PATH
    if [ ! -d $MODULES_INSTALL_PATH ] ; then
        echo "Couldn't create webcam modules install directory $MODULES_INSTALL_PATH"
    fi
    make -k ARCH=arm CROSS_COMPILE=${TOOLCHAIN}- modules
    make -k ARCH=arm CROSS_COMPILE=${TOOLCHAIN}- \
        INSTALL_MOD_PATH=$MODULES_INSTALL_PATH modules_install
    echo "Copy modules to Raspberry to /lib/modules/$KERNEL_VERSION"
fi

cd $CUR_DIR
